package functionality;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import data.Data;
import data.IData;

public class Transmitter implements ITransmitter {

	private volatile Lock lock;
	private IData data;

	public Transmitter(IData data){
		lock = new ReentrantLock();
		this.data = data;
	}

	/* (non-Javadoc)
	 * @see src.ITransmitter#getUserInputs(java.io.PrintWriter, java.io.BufferedReader)
	 */
	@Override
	public int getInputUserId(PrintWriter out, BufferedReader in){	
		int userID = 0;
			try {
				if (rm20(out, in, "4 ", "\"Indtast \"", "\"dit \"", "\"brugerID\"")){
					userID = Integer.parseInt(secondAnswer(out, in));
				}
			} catch (IOException e) {
				System.out.println("Operatør-login tråd fejlet");
			} catch (NumberFormatException e) {
				System.out.println("Bruger har indtastet ugyldigt bruger ID");
			}
			return userID;
		}
	
	@Override
	public String getInputPassword(PrintWriter out, BufferedReader in){	
		String password = "";
			try {
				if (rm20(out, in, "4 ", "\"Indtast \"", "\"dit \"", "\"password\"")){
					password = secondAnswer(out, in);
				}
			} catch (IOException e) {
				System.out.println("Operatør-login tråd fejlet");
			} catch (NumberFormatException e) {
				System.out.println("Bruger har indtastet ugyldigt bruger ID");
			}
			return password;
		}
	

	/* (non-Javadoc)
	 * @see src.ITransmitter#rm20(java.io.PrintWriter, java.io.BufferedReader, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public boolean rm20(PrintWriter out, BufferedReader in, String n, String prompt1, String prompt2, String prompt3) throws IOException {
		String reply = "";
		out.println("RM20 " + n + prompt1 + prompt2 + prompt3);
		reply = in.readLine();
		if (reply.equalsIgnoreCase("RM20 B")){
			return true;
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see src.ITransmitter#weightAnswer(java.io.PrintWriter, java.io.BufferedReader)
	 */
	@Override
	public String secondAnswer(PrintWriter out, BufferedReader in) throws IOException{ 
		String output = "";
		String reply = in.readLine();
		String r1 = reply.substring(0, 6);
		if (r1.equalsIgnoreCase("RM20 A")){
			output = reply.substring(7).trim();
		} else {
			output = "Bruger afbrudt.";
		}
		return output;	
	}	

	/* (non-Javadoc)
	 * @see src.ITransmitter#dispWghtScl(java.io.PrintWriter, java.io.BufferedReader)
	 */
	@Override
	public String dispWghtScl(PrintWriter out, BufferedReader in) throws IOException {
		String reply = "";
		if (lock.tryLock()){
			try {
				String dispWghtScl = "DW";
				out.println(dispWghtScl);
				reply = in.readLine();
				if (!reply.substring(0, 2).equalsIgnoreCase("ES")){
					reply = "Vægt opdateret.";
				} else {
					reply = "Kommando fejlet.";
				}
			} finally {
				lock.unlock();
			}
		}else {
			reply = "Vægt optaget.";
		}
		return reply;
	}

	/* (non-Javadoc)
	 * @see src.ITransmitter#dispTxtScl(java.io.PrintWriter, java.io.BufferedReader)
	 */
	@Override
	public String dispTxtScl(PrintWriter out, BufferedReader in, String text) throws IOException {
		String reply = "";
		String dispTxtScl = "D ";
		out.println(dispTxtScl + text);
		reply = in.readLine();
		if (!reply.substring(0, 2).equalsIgnoreCase("ES")){
			reply = "Displaytekst ændret.";
		} else {
			reply = "Kommando fejlet.";
		}	
		return reply;
	}

	/* (non-Javadoc)
	 * @see src.ITransmitter#zeroScale(java.io.PrintWriter, java.io.BufferedReader)
	 */
	@Override
	public String zeroScale(PrintWriter out, BufferedReader in) throws IOException{
		String zeroScale = "Z";
		String reply = "";
		out.println(zeroScale);
		reply = in.readLine();
		if (!reply.substring(0, 2).equalsIgnoreCase("ES")){
			reply = "Vægt nulstillet.";
		} else {
			reply = "Kommando fejlet.";
		}
		return reply;
	}

	/* (non-Javadoc)
	 * @see src.ITransmitter#tareScale(java.io.PrintWriter, java.io.BufferedReader)
	 */
	@Override
	public String tareScale(PrintWriter out, BufferedReader in) throws IOException{
		String tareScale = "T";
		String reply = "";
		out.println(tareScale);
		reply = in.readLine();
		if (!reply.substring(0, 2).equalsIgnoreCase("ES")){
			reply = "Tara Vægt: " + reply.substring(4);
		} else {
			reply = "Kommando fejlet.";
		}
		return reply;
	}

	/* (non-Javadoc)
	 * @see src.ITransmitter#readScale(java.io.PrintWriter, java.io.BufferedReader)
	 */
	@Override
	public String readScale(PrintWriter out, BufferedReader in) throws IOException{
		String readScale = "S";
		String reply = "";
		out.println(readScale);
		reply = in.readLine();
		if (!reply.substring(0, 2).equalsIgnoreCase("ES")){
			reply = "Vægt: " + reply.substring(4);
		} else {
			reply = "Kommando fejlet.";
		}
		return reply;
	}
	
	@Override
	public Lock getLock(){
		return this.lock;
	}
	
	@Override
	public String getOprPassword(int id){
		int p = -1;
		int[] oprID = data.getOprID();
		for (int i=0; i<oprID.length; i++){
			if (oprID[i] == id){
				i = p;
			} 
		}
		String[] oprPass = data.getOprPassword();
		return oprPass[p];
	}
}
